package top.xchch.tool.mockito.juejin;

import org.junit.Test;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.verification.Timeout;
import org.mockito.verification.VerificationMode;

import static org.mockito.Mockito.*;


/**
 * 超时验证
 *
 * @author sai.zhang
 * @date 2021-08-31 15:56
 */
public class TimeoutTest18 {

    @Test
    public void testTimeout() throws Exception {

        TimeMockTest mock = mock(TimeMockTest.class);
        //测试程序将会在下面这句阻塞100毫秒，timeout的时候再进行验证是否执行过someMethod()
        verify(mock, timeout(1000)).someMethod();
        //和上面代码等价
        verify(mock, timeout(100).times(1)).someMethod();

        //阻塞100ms，timeout的时候再验证是否刚好执行了2次
        verify(mock, timeout(100).times(2)).someMethod();

        //timeout的时候，验证至少执行了2次
        verify(mock, timeout(100).atLeast(2)).someMethod();

        //timeout时间后，用自定义的检验模式验证someMethod()
        VerificationMode yourOwnVerificationMode = new VerificationMode() {
            @Override
            public void verify(VerificationData data) {
                // TODO: 2016/12/4 implement me
                System.out.println("verify");
            }

            @Override
            public VerificationMode description(String s) {
                System.out.println("description");
                return null;
            }
        };
        verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
    }

}

interface TimeMockTest {
    void someMethod();
}
